
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[EBAS_SP_GEM_DATO_NOTAT]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[EBAS_SP_GEM_DATO_NOTAT]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE [DBO].[EBAS_SP_GEM_DATO_NOTAT] 
	@session varchar(255),
	@studGuid varchar(255),
	@dato varchar(255),
	@emne varchar(255),
	@filNavn varchar(255),
	@notat image = NULL
WITH ENCRYPTION
AS
	DECLARE 
		@rettighed int,
		@antal int,
		@guid uniqueidentifier
	
	SET NOCOUNT ON

	CREATE TABLE #temp ( rett int )
	INSERT INTO #temp (rett)
	EXEC EBAS.DBO.EBAS_SP_SIKKERHED_VERIFICER_SESSION @session	

	SET @rettighed = (SELECT IsNull(max(rett),0) FROM #temp)

	IF 8 < @rettighed 
	BEGIN
		SET @guid = NEWID();
		
		IF @notat IS NOT NULL
		BEGIN
			INSERT INTO EBAS_DATO_NOTAT
			SELECT @guid, GetDate(), @emne, @filNavn, @notat, @studguid
		END
		ELSE
		BEGIN
			INSERT INTO EBAS_DATO_NOTAT (GUID, DATO, EMNE, FIL_NAVN, STUDERENDE_GUID)
			SELECT @guid, GetDate(), @emne, @filNavn, @studguid
		END		
		
		SELECT GUID, DATO, EMNE, FIL_NAVN, NOTAT_BINARY, STUDERENDE_GUID, 1 AS ANTAL
		FROM EBAS_DATO_NOTAT
		WHERE GUID = @guid
	END
	ELSE
		SELECT GUID, DATO, EMNE, FIL_NAVN, NOTAT_BINARY, STUDERENDE_GUID, 0 AS ANTAL
		FROM EBAS_DATO_NOTAT
		WHERE GUID IS NULL
		ORDER BY DATO DESC	
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
